<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="130px">
			<el-form-item label="所属单位" prop="unit">
				<el-select v-model="FormData.unit" placeholder="请选择所属单位">
					<el-option v-for="(item,index) in unitList" :key="index" :value="item.id" :label="item.name" />
				</el-select>
			</el-form-item>
			<el-form-item label="联系人" prop="linkName">
				<el-input v-model="FormData.linkName" placeholder="请输入联系人" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系电话" prop="linkTel">
				<el-input v-model="FormData.linkTel" placeholder="请输入联系电话" clearable></el-input>
			</el-form-item>
			<el-form-item label="合同签约时间" prop="startTime">
				<el-date-picker v-model="FormData.startTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" type="date"
					placeholder="请选择合同签约时间" />
			</el-form-item>
			<el-form-item label="合同到期时间" prop="endTime">
				<el-date-picker v-model="FormData.endTime" value-format="YYYY-MM-DD" type="date"
					placeholder="请选择合同到期时间" />
			</el-form-item>
			<el-form-item label="上传合同扫描件" prop="">
				<sc-upload-file v-model="FormData.files" :limit="10"
					tip="最多上传10个文件,单个文件不要超过10M,请上传jpg、png、docx或pdf格式文件">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
			</el-form-item>
			<!-- <el-form-item label="是否保险单" prop="insure">
				<el-switch v-model="FormData.insure" />
			</el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				fileurlArr: [],
				//表单数据
				FormData: {
					files: [],
					insure: false
				},
				//验证规则
				rules: {
					unit: [{
						required: true,
						message: '请输入姓名'
					}],
					linkName: [{
						required: true,
						message: '请输入联系人'
					}],
					linkTel: [{
						required: true,
						message: '请输入联系电话'
					}],
					startTime: [{
						required: true,
						message: '合同签约时间'
					}],
					endTime: [{
						required: true,
						message: '合同到期时间'
					}],
					files: [{
						required: true,
						message: '请上传合同扫描件'
					}],
					insure: [{
						required: true,
						message: '是否保险单'
					}],
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				aeraList: [{
					value: 'guide',
					label: '海南省',
					children: [{
						value: 'disciplines',
						label: '海口市',
						children: [{
							value: 'consistency',
							label: '美兰区',
						}],
					}, ],
				}],
				unitList: []
			}
		},
		watch: {
			'form.address'(nVal) {
				if (!nVal) {
					this.showAMap = false
				} else {
					this.showAMap = true
				}
			}
		},
		mounted() {
			this.getUnit()
		},
		methods: {
			async getUnit() {
				let data = {
					page: 1,
					pagesize: 100
				}
				const res = await this.$API.enterprise.unit.list.post(data)
				if (res.data.datas) {
					this.unitList = res.data.datas
				}
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true
				let _arr = []
				if (this.FormData.files.length) {
					this.FormData.files.forEach(item => {
						_arr.push(item.url)
					})
				}
				this.FormData.files = _arr.join(",")
				this.FormData.unit = {
					id: this.FormData.unit
				}
				console.log(this.FormData)
				const res = await this.$API.contract.agree.save.post(this.FormData)
				this.isSaveing = false;
				if (res.code == 200) {

					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.FormData.id = data.id
				if (data.unitId) {
					this.FormData.unit = data.unitId
				}
				this.FormData.linkName = data.linkName
				this.FormData.linkTel = data.linkTel
				this.FormData.startTime = data.startTime
				this.FormData.endTime = data.endTime
				this.FormData.insure = data.insure
				if (data.files) {
					let files = data.files.split(",")
					const newArray = files.map(item => {
						return {
							name: item,
							url: item
						}
					})
					this.FormData.files = newArray
					console.log(12333, newArray)
				}
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
			}
		}
	}
</script>

<style>
</style>